package com.rickpan.entity;

import jakarta.persistence.*;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * VIP订阅实体类
 */
@Entity
@Table(name = "vip_subscriptions", indexes = {
    @Index(name = "idx_user_id", columnList = "userId"),
    @Index(name = "idx_status", columnList = "status"),
    @Index(name = "idx_end_date", columnList = "endDate"),
    @Index(name = "idx_user_status", columnList = "userId, status"),
    @Index(name = "idx_start_end_date", columnList = "startDate, endDate")
})
@Data
public class VipSubscription {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 用户ID
     */
    @Column(nullable = false)
    private Long userId;

    /**
     * 关联支付订单ID
     */
    private Long orderId;

    /**
     * 套餐类型
     */
    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private PaymentOrder.PlanType planType;

    /**
     * 开始日期
     */
    @Column(nullable = false)
    private LocalDate startDate;

    /**
     * 结束日期
     */
    @Column(nullable = false)
    private LocalDate endDate;

    /**
     * 订阅状态
     */
    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private SubscriptionStatus status = SubscriptionStatus.ACTIVE;

    /**
     * 是否自动续费
     */
    @Column(nullable = false)
    private Boolean autoRenew = false;

    /**
     * 原始金额
     */
    @Column(precision = 10, scale = 2)
    private BigDecimal originalAmount;

    /**
     * 优惠金额
     */
    @Column(precision = 10, scale = 2)
    private BigDecimal discountAmount = BigDecimal.ZERO;

    /**
     * 实际支付金额
     */
    @Column(precision = 10, scale = 2)
    private BigDecimal actualAmount;

    /**
     * 创建时间
     */
    @Column(nullable = false, updatable = false)
    private LocalDateTime createdAt;

    /**
     * 更新时间
     */
    @Column(nullable = false)
    private LocalDateTime updatedAt;

    /**
     * 备注信息
     */
    @Column(length = 500)
    private String remark;

    @PrePersist
    protected void onCreate() {
        createdAt = LocalDateTime.now();
        updatedAt = LocalDateTime.now();
    }

    @PreUpdate
    protected void onUpdate() {
        updatedAt = LocalDateTime.now();
    }

    /**
     * 订阅状态枚举
     */
    public enum SubscriptionStatus {
        ACTIVE("生效中"),
        EXPIRED("已过期"),
        CANCELLED("已取消");

        private final String description;

        SubscriptionStatus(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }
}